package top.ajunnihao.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ajunnihao.core.entity.Message;
import top.ajunnihao.core.mapper.MessageMapper;
import top.ajunnihao.core.service.MessageService;
import top.ajunnihao.core.util.page.PageInfo;
import top.ajunnihao.core.util.resp.BaseResp;
import top.ajunnihao.core.util.resp.JResp;

import java.util.List;

/**
 * TODO 帖子业务层接口实现类
 *
 * @author AJun
 * @version 1.0.0
 * @date 2020/5/24
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public PageInfo<Message> findMessageByPage(Integer pageNum, Integer pageSize) {
        PageInfo<Message> pageInfo = new PageInfo<>(pageNum, pageSize);
        // 查询总记录数
        pageInfo.setTotal(messageMapper.findMessageCount());
        // 查询每页要显示的记录内容
        int pageStart = pageInfo.getPageStart();
        pageInfo.setList(messageMapper.findMessageByPage(pageStart, pageSize));
        // 分页
        pageInfo.startPage();
        return pageInfo;
    }

    @Override
    public List<Message> findNiceMsg() {
        return messageMapper.findNiceMsg();
    }

    @Override
    public List<Message> findNewMsg() {
        return messageMapper.findNewMsg();
    }

    @Override
    public PageInfo<Message> findMessageReplyByPage(Long msgId, Integer pageNum, Integer pageSize) {
        PageInfo<Message> pageInfo = new PageInfo<>(pageNum, pageSize);
        // 查询总记录数
        pageInfo.setTotal(messageMapper.findMessageReplyCount(msgId));
        // 查询每页要显示的记录内容
        int pageStart = pageInfo.getPageStart();
        pageInfo.setList(messageMapper.findMessageReplyByPage(msgId, pageStart, pageSize));
        // 分页
        pageInfo.startPage();
        return pageInfo;
    }

    @Override
    public Message findMessageById(Long msgId) {
        return messageMapper.findMessageById(msgId);
    }

    @Override
    public boolean saveMessage(Message message) {
        return messageMapper.saveMessage(message) > 0;
    }

    @Override
    public boolean updateMessage(Message message) {
        return messageMapper.updateMessage(message) > 0;
    }

    @Override
    public BaseResp setMsgFlags(String cmd, Long msgId) {
        // 查询帖子原本的标志位
        Message message = messageMapper.findMessageFlag(msgId);
        // 标志位取反(置顶则取消，取消则置顶)
        Integer topFlag = (message.getTopFlag() == 0) ? 1 : 0;
        Integer niceFlag = (message.getNiceFlag() == 0) ? 1 : 0;

        // 操作: 设置 topFlag/niceFlag/delFlag 标识
        boolean okFlag;
        if ("top".equals(cmd)) {
            // 置顶操作
            okFlag = messageMapper.updateMsgTopFlag(msgId, topFlag) > 0;
            if (okFlag) {
                return JResp.success("置顶成功");
            }
            return JResp.fail("置顶失败");
        } else if ("nice".equals(cmd)) {
            // 加精操作
            okFlag = messageMapper.updateMsgNiceFlag(msgId, niceFlag) > 0;
            if (okFlag) {
                return JResp.success("加精成功");
            }
            return JResp.fail("加精失败");
        } else if ("del".equals(cmd)) {
            // 删贴操作
            okFlag = messageMapper.updateMsgDelFlag(msgId) > 0;
            if (okFlag) {
                return JResp.success("删贴成功");
            }
            return JResp.fail("删贴失败");
        }
        return JResp.fail("不支持此操作");
    }

}
